<?php

class Materials_Model_Material_Observer_SearchContentUpdater extends Workset_Model_Observer_Abstract {

    protected $_isStored;

    protected $_options = array(
        'check_pub_status' => false,
        'title_fields' => array(
            'title', 'category', 'tags'
        )
    );

    public function checkPublishStatus($flag) {
        $this->_options['check_pub_status'] = (bool)$flag;
    }

    public function beforeSave(Workset_Model_Subject_Interface $material, $data = null) {
        $this->_isStored = null;

        if ($material instanceof Materials_Model_Material) {
            $this->_isStored = $material->isStored();
        }
    }

    public function afterSave(Workset_Model_Subject_Interface $material, $data = null) {
        if ($material instanceof Materials_Model_Material) {

            $updateContent = true;
            $deleteContent = false;

            if ($this->_options['check_pub_status']) {

                if (!$material->isPublished()) {
                    $updateContent = false;

                    if ($this->_isStored) {
                        $deleteContent = true;
                    }
                }

            }

            //обновление контента в индексе
            if ($updateContent) {

                $title = array();

                foreach ($this->_options['title_fields'] as $field) {
                    switch($field) {
                        case 'category':
                            $category = $material->getCategory();

                            if ($category instanceof Materials_Model_Category) {
                                $title[] = $category['name'];
                            }
                        break;

                        default:
                           $title[] = $material[$field];
                    }
                }

                $title = implode(' ', $title);

                $content = array(
                    'title' => $title,
                    'content' => $this->_getContent($material),
                    'created' => new Zend_Db_Expr("FROM_UNIXTIME({$material['created']})"),
                    'category_id' => $material->getCategoryId(),
                    'status' => (int) $material['status']
                );

                if ($this->_isStored) {
                    $material->updateSearchContent($content);
                } else {
                    $material->addSearchContent($content);
                }
                
            } elseif ($deleteContent) {
                $material->deleteSearchContent();
            }

        }

        $this->_isStored = null;
    }

    protected function _getContent(Materials_Model_Material $material) {
        $content = array();

        if ($material->isContentSerialized()) {
            $blocks = $material->getContent()->filter(array('text', 'textarea'));

            $it = new RecursiveIteratorIterator($blocks, RecursiveIteratorIterator::CHILD_FIRST);
            $it->setMaxDepth(1);

            foreach ($it as $key => $item) {
                if ($it->getDepth() == $it->getMaxDepth()) {
                    $content[] = $item->toString();
                }
            }

        } else {
            $content[] = $material->getContent();
        }

        $category = $material->getCategory();

        if ($category) {
            array_unshift($content, $category['name']);
        }

        if (isset($material['tags'])) {
            $tags = array_map('trim', explode(',', $material['tags']));
            $content = array_merge($content, $tags);
        }

        return strip_tags( implode(' ', $content) );
    }

}